(function(){
	angular.module("webApp").controller("ShopCtrl",ShopCtrl);
	
	ShopCtrl.$inject=['$scope','dataService','$rootScope','$timeout','$state','$location','helper','sessionService','$filter']
	function ShopCtrl($scope,dataService,$rootScope,$timeout,$state,$location,helper,sessionService,$filter){
		

		$scope.shopInit=function(){
		  var seller_userid = $location.search().seller_uid;
		  var store_id=$location.search().shop_id;
		  var query={};

		  if(seller_userid){
		  	query.query={partner_id:seller_userid};
		  }
		  if(store_id){
		  	query.query={store_id:store_id};
		  }
		  $scope.pageSize =4;
		  $scope.pageNo=1;
		  dataService.storeGet(query).then(function(r){
		  	if(r.Msg){
		  		helper.myAlert("error:"+r.Msg);
		  	}else{
		  		$scope.shop=r.store[0];
		  		
		  		var itemquery={page_no:$scope.pageNo,page_size:$scope.pageSize};
		  		//itemquery
		  		$scope.getItemList(itemquery).then(function(result){
		  			$scope.itemsList=result.items;
		  			$scope.itemsListTotal_count=result.total_count;
		  		})
		  	}
		  })
		}
		var loading = false;

      $(document.body).infinite(2).on("infinite", function() {
      	console.log("22");
        if(loading) return;
        loading = true;
        setTimeout(function() {
        	if($scope.pageSize*$scope.pageNo<=$scope.itemsListTotal_count){
        		$scope.pageNo+=1;
        		console.log("!11");
		  		var itemquery={page_no:$scope.pageNo,page_size:$scope.pageSize};
	        	$scope.getItemList().then(function(result){
	        		// $scope.itemsList.concat(result.items);
	        		_.map(result.items,function(item){
	        			$scope.itemsList.push(item);
	        		})
	        	});
	          loading = false;
        	}
        	
        }, 2000);
      });
		$scope.getItemList=function(){
			 var query={ page_no: $scope.pageNo ? $scope.pageNo : "1", page_size: $scope.pageSize };
			// var query={};
			query.query={partner_id:$scope.shop.partner_id}
		return  dataService.itemGet(query).then(function(result){
		  			return result;
		  		})
		}
		

	}
})();